package tech.jabari.user.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import tech.jabari.user.dao.UserDao;
import tech.jabari.user.dao.impl.UserDaoImpl;
import tech.jabari.user.entity.User;
import tech.jabari.user.service.UserService;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jabari Lu
 * @title: UserServiceImpl
 * @data 2020/8/4 14:23 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();
    @Override
    public String selectAllUser(Map<String,Object> paramsMap) {
        // 获取分页参数，进行相应的逻辑处理
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        Integer pageNo = (Integer) paramsMap.get("pageNo");
        Integer beginNo = (pageNo - 1) * pageSize;
        paramsMap.put("beginNo", beginNo);
       /* String username = (String)paramsMap.get("username");*/
        // 查询用户列表信息。
        List<User> userList = userDao.findAllUser(paramsMap);
        // 查询用户的总记录数。
        Integer totalRecords = userDao.findAllUserCount(paramsMap);
        // 计算总页数
        Integer pageNos = totalRecords / pageSize;
        if(totalRecords % pageSize!=0){
            pageNos = pageNos + 1;
       }
        /**
         * "pageNos":3,
         * "totalRecords": 15,
         * "pageSize":5
         */
        ObjectMapper objectMapper = new ObjectMapper();
        // 设置JSON中的日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(df);

        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("pageSize",pageSize);
        dataMap.put("totalPageNum",pageNos);// 总页数
        dataMap.put("pageNo",pageNo);// 当前页码
        dataMap.put("totalRecords",totalRecords);
        dataMap.put("items",userList);
        String json = "";
        try {
            json += objectMapper.writeValueAsString(dataMap);

        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return json;
    }


    @Override
    public Integer delUser(String id) {
        // 调用DAO删除用户
        Integer idInt = Integer.parseInt(id);
        Integer res = userDao.deleteUser(idInt);
        return res;
    }



    @Override
    public int batchDelUser(String delIds) {
        Integer res = userDao.batchDeleteUser(delIds);
        return res;
    }


    @Override
    public String findUserById(String id) {
        Integer idInt = Integer.parseInt(id);
        User user = userDao.findById(idInt);
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        objectMapper.setDateFormat(sdf);
        String s = null;
        try {
            s = objectMapper.writeValueAsString(user);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return s;
    }
}
